package com.whh.servlet.user;

import com.whh.pojo.User;
import com.whh.service.user.UserService;
import com.whh.service.user.UserServiceImpl;
import com.whh.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet.....start....");
        //获得用户名密码
        String userCode=req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        //和数据库密码进行对比，调用业务层
        UserService userService = new UserServiceImpl();
        User user = userService.login(userCode);
        if (user!=null&&userPassword.equals(user.getUserPassword())){//查有此人 登录
            //将信息放在session中
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            //跳转倒主页
           resp.sendRedirect(req.getContextPath()+"/jsp/frame.jsp");
        }else {
            //查无此人 转发回登录页
            req.setAttribute("error","用户名或者密码不正确");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
